package com.yoda.mall.mysql.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yoda.mall.api.SkuStockApi;
import com.yoda.mall.model.entity.pms.SkuStock;
import com.yoda.mall.mysql.mapper.SkuStockMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shawn
 * @date 2022年 12月 18日 20:34
 */
@DubboService
public class SkuStockApiImpl implements SkuStockApi {

    @Resource
    private SkuStockMapper skuStockMapper;

    @Override
    public List<SkuStock> getSkuByProductId(Long productId, String keyword) {
        LambdaQueryWrapper<SkuStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuStock::getProductId,productId);
        wrapper.like(StringUtils.isNotEmpty(keyword),SkuStock::getSkuCode,keyword);
        return skuStockMapper.selectList(wrapper);
    }

    @Override
    @Transactional
    public Integer updateProductSkuStock(List<SkuStock> skuStockList) {
        for (SkuStock skuStock : skuStockList) {
                skuStockMapper.updateById(skuStock);
        }
        return skuStockList.size();
    }
}
